<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Admin\Traits\AdminTrait;
use App\Http\Requests\Admin\BaseRequest;
use App\Models\Admin\AdminImg;
use App\Models\Admin\AdminImgCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class ImgController extends AdminController
{
    use AdminTrait;
    protected $msg=[
        'name.unique'=>'名称已存在'
    ];
    public function categoryList(BaseRequest $request)
    {
        $list=AdminImgCategory::getList();
        return $this->success($list);
    }

    public function imgList(BaseRequest $request)
    {
        $input=$request->all();
        //过滤数据
        $this->vali_rules['category_id']='required|integer';
        $validator = Validator::make($input, $this->vali_rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $this->getPageAndSize($input);
        $list=AdminImg::getList($input,$this->skip,$this->size);
        $count=AdminImg::getCount($input);
        $data=[
            'list'=>$list,
            'count'=>$count,
        ];
        return  $this->success($data);
    }

    public function addCategory(BaseRequest $request)
    {
        $input=$request->only('pid','name');
        $rules=[
            'pid'=>[
                'bail','required',
                Rule::exists('admin_img_category','id')->where('pid',0),
            ],
            'name'=>[
                'required',
//                Rule::unique('admin_img_category','name')->where('pid', $input['pid'])
            ]
        ];
        $validator = Validator::make($input, $rules,$this->msg+$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        DB::beginTransaction();
        try {
            $info=AdminImgCategory::create($input);
            AdminImgCategory::addLog($info->toArray(),[],1);
            DB::commit();
            return  $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return $this->error();
        }
    }

    public function editCategory(BaseRequest $request)
    {
        $input=$request->only('id','name');
        $rules=[
            'id'=>[
                'bail','required',
                Rule::exists('admin_img_category','id')->where(function ($query)
                {
                    $query->where('pid','!=',0);
                }),
            ],
            'name'=>[
                'required',
//                Rule::unique('admin_img_category','name')->ignore($input['id'])
            ]
        ];
        $validator = Validator::make($input, $rules,$this->msg+$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        DB::beginTransaction();
        try {
            $info=AdminImgCategory::find($input['id']);
            $perInfo = $info->getOriginal();//获取修改之前的字段
            $info->name=$input['name'];
            $info->save();
            $nextInfo = $info->getChanges();//获取修改之后的字段
            AdminImgCategory::addLog($perInfo,$nextInfo,2);
            DB::commit();
            return  $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return $this->error();
        }
    }

    public function deleteCategory(BaseRequest $request)
    {
        $input=$request->all();
        $rules=[
            'id'=>[
                'bail','required',
                Rule::exists('admin_img_category','id')->where(function ($query)
                {
                    $query->where('pid','!=',0);
                }),
            ],
        ];
        $validator = Validator::make($input,$rules ,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $id=$input['id'];
        $is_img=AdminImg::where('category_id',$id)->count();
        if($is_img)
        {
            return  $this->error(['-1','该分类下还有图片']);
        }
        DB::beginTransaction();
        try {
            $info=AdminImgCategory::find($id);
            $info->delete();
            AdminImgCategory::addLog($info->toArray(),[],3);
            DB::commit();
            return  $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return  $this->error();
        }
    }

    public function addImg(BaseRequest $request)
    {
        $input=$request->only('img','category_id','remark');
        $rules=[
            'category_id'=>[
                'bail','required',
                Rule::exists('admin_img_category','id')->where(function ($query)
                {
                    $query->where('pid','!=',0);
                }),
            ],
            'img'=>[
                'required',
            ],
            'remark'=>'string'
        ];
        $validator = Validator::make($input, $rules,$this->msg+$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        DB::beginTransaction();
        try {
            $info=AdminImg::create($input);
            AdminImg::addLog($info->toArray(),[],1);
            DB::commit();
            return  $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return $this->error(['-1',$exception->getMessage()]);
        }
    }

    public function deleteImg(BaseRequest $request)
    {
        $input=$request->only('id');
        $rules=[
            'id'=>[
                'bail','required',
                Rule::exists('admin_img','id')
            ],
        ];
        $validator = Validator::make($input, $rules,$this->msg+$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $id=$input['id'];
        DB::beginTransaction();
        try {
            $info=AdminImg::find($id);
            $pre=$info->getOriginal();
            $info->status=0;
            $info->save();
            $next=$info->getChanges();
            AdminImg::addLog($pre,$next,3);
            DB::commit();
            return  $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return $this->error();
        }
    }

}
